    //
//  UserProfileController.m
//  SpendFunk
//
//  Created by bm on 4/22/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "UserProfileController.h"
#import "SpendFunkAppDelegate.h"
#import "image.h"

@implementation UserProfileController

#pragma mark -
#pragma mark Initialization
-(id)init{
	m_pArrContent = [[NSMutableArray alloc] initWithObjects:@"View Purchases", @"View Reports", @"54 Followers", @"54 Following", nil];

	return self;
}

-(void)loadView{
	[super loadView];
	isFollow = FALSE;
	[self initView];
}

-(void)initView{
	NSString *path = nil;
	UIImage *img = nil;
	//BKGR
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BKGR_VIEW];
	img = [UIImage imageNamed:path];
	UIImageView *imgvBkgr = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 320, 480)];
	[imgvBkgr setImage:img];
	[self.view addSubview:imgvBkgr];
	[imgvBkgr setUserInteractionEnabled:YES];
	[imgvBkgr release];
	
	//bkgr user
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BKGR_USERPROFILE];
	img = [UIImage imageNamed:path];	
	UIImageView *imgvBkgrUser = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, img.size.width, img.size.height)];
	[imgvBkgrUser setImage:img];
	[imgvBkgrUser setUserInteractionEnabled:YES];
	[imgvBkgrUser setBackgroundColor:[UIColor redColor]];
	[self.view addSubview:imgvBkgrUser];
	[imgvBkgrUser release];
	
	//image user
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BORDER];
	img = [UIImage imageNamed:path];	
	UIImageView *imgvImageUserBkgr = [[UIImageView alloc] initWithFrame:CGRectMake(5, 25, img.size.width, img.size.height)];
	[imgvImageUserBkgr setImage:img];
	[imgvImageUserBkgr setUserInteractionEnabled:YES];
	[imgvImageUserBkgr setBackgroundColor:[UIColor redColor]];
	[self.view addSubview:imgvImageUserBkgr];
	[imgvImageUserBkgr release];
	
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BKGR_USER];
	img = [UIImage imageNamed:path];	
	UIImageView *imgvImageUser = [[UIImageView alloc] initWithFrame:CGRectMake(8.5, 8.5, img.size.width, img.size.height)];
	[imgvImageUser setImage:img];
	[imgvImageUser setUserInteractionEnabled:YES];
	[imgvImageUser setBackgroundColor:[UIColor redColor]];
	[imgvImageUserBkgr addSubview:imgvImageUser];
	[imgvImageUser release];
	
	//BACK
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BTN_BACK];
	img = [UIImage imageNamed:path];	
	m_pBtnBack = [[UIButton alloc] initWithFrame:CGRectMake(5, -14, img.size.width, img.size.height)];
	[m_pBtnBack setBackgroundImage:img forState:UIControlStateNormal];
	[m_pBtnBack setBackgroundColor:[UIColor clearColor]];
	[m_pBtnBack setTitle:@"Back" forState:UIControlStateNormal];
	m_pBtnBack.titleLabel.textColor = [UIColor whiteColor];
	[m_pBtnBack addTarget:nil action:@selector(onClickBack) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:m_pBtnBack];
	
	///done
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BTN_NEXT];
	img = [UIImage imageNamed:path];	
	m_pBtnAccount = [[UIButton alloc] initWithFrame:CGRectMake(320-5-img.size.width, -14, img.size.width, img.size.height)];
	[m_pBtnAccount setBackgroundImage:img forState:UIControlStateNormal];	
	[m_pBtnAccount setBackgroundColor:[UIColor clearColor]];
	[m_pBtnAccount setTitle:@"Account" forState:UIControlStateNormal];
	[m_pBtnAccount addTarget:nil action:@selector(onClickAccount) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:m_pBtnAccount];
	
	
	//label infomation about user
	m_pLblName = [[UILabel alloc] initWithFrame:CGRectMake(140, 25, 100, 30)];
	[m_pLblName setBackgroundColor:[UIColor clearColor]];
	[m_pLblName setTextColor:[UIColor blueColor]];
	[m_pLblName setFont:[UIFont fontWithName:@"Arial" size:18]];
	[self.view addSubview:m_pLblName];
	//[m_pLblName release];
	
	m_pLblLastAction = [[UILabel alloc] initWithFrame:CGRectMake(140, 48, 100, 20)];
	[m_pLblLastAction setBackgroundColor:[UIColor clearColor]];
	[m_pLblLastAction setTextColor:[UIColor blueColor]];
	[m_pLblLastAction setText:@"Last Action :"];
	[m_pLblLastAction setFont:[UIFont fontWithName:@"Arial" size:14]];
	[self.view addSubview:m_pLblLastAction];
	[m_pLblLastAction release];
	
	m_pLblMoney = [[UILabel alloc] initWithFrame:CGRectMake(140, 60, 100, 30)];
	[m_pLblMoney setBackgroundColor:[UIColor clearColor]];
	[m_pLblMoney setTextColor:[UIColor orangeColor]];
	[m_pLblMoney setFont:[UIFont fontWithName:@"Arial" size:18]];
	[self.view addSubview:m_pLblMoney];
	//[m_pLblMoney release];
	
	/// Follow
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BTN_FOLLOW];
	img = [UIImage imageNamed:path];	
	m_pBtnFollow = [[UIButton alloc] initWithFrame:CGRectMake(140, 180-25-img.size.height, img.size.width, img.size.height)];
	[m_pBtnFollow setBackgroundImage:img forState:UIControlStateNormal];	
	[m_pBtnFollow setBackgroundColor:[UIColor clearColor]];
	[m_pBtnFollow setTitle:@"Follow" forState:UIControlStateNormal];
	[m_pBtnFollow addTarget:nil action:@selector(onClickFollow) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:m_pBtnFollow];
	
//view seperate button
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BKGR_SEPERATE_BTN];
	img = [UIImage imageNamed:path];	
	UIImageView *imgvBtnSeperate = [[UIImageView alloc] initWithFrame:CGRectMake(1, 180-15, img.size.width, img.size.height)];
	[imgvBtnSeperate setImage:img];
	[imgvBtnSeperate setUserInteractionEnabled:YES];
	[self.view addSubview:imgvBtnSeperate];
	[imgvBtnSeperate release];
	
	int nX=0,nY=0,nW=0,nH=0;
	//label item
	nX=8;nY=8;nW=100;nH=20;
	m_pLblHigh = [[UILabel alloc] initWithFrame:CGRectMake(nX, nY, nW, nH)];
	[m_pLblHigh setBackgroundColor:[UIColor clearColor]];
	[m_pLblHigh setTextColor:[UIColor blueColor]];
	[m_pLblHigh setText:@"HIGH"];
	[m_pLblHigh setFont:[UIFont fontWithName:@"Arial" size:16]];
	[imgvBtnSeperate addSubview:m_pLblHigh];
	[m_pLblHigh release];
	
	m_pLblHighNumber = [[UILabel alloc] initWithFrame:CGRectMake(nX, nY+15, nW, nH+15)];
	[m_pLblHighNumber setBackgroundColor:[UIColor clearColor]];
	[m_pLblHighNumber setTextColor:[UIColor blueColor]];
	[m_pLblHighNumber setText:@"10"];
	[m_pLblHighNumber setFont:[UIFont fontWithName:@"Arial" size:28]];
	[imgvBtnSeperate addSubview:m_pLblHighNumber];
	[m_pLblHighNumber release];
	
	m_pBtnHigh = [[UIButton alloc] initWithFrame:CGRectMake(nX-8, nY-8, img.size.width, img.size.height)];
	[m_pBtnHigh setBackgroundColor:[UIColor clearColor]];
	[m_pBtnHigh addTarget:nil action:@selector(onClickHigh) forControlEvents:UIControlEventTouchUpInside];
	[imgvBtnSeperate addSubview:m_pBtnHigh];
	//////////////////////
	nX=nX+106;
	m_pLblBadges = [[UILabel alloc] initWithFrame:CGRectMake(nX, nY, nW, nH)];
	[m_pLblBadges setBackgroundColor:[UIColor clearColor]];
	[m_pLblBadges setTextColor:[UIColor blueColor]];
	[m_pLblBadges setText:@"BADGES"];
	[m_pLblBadges setFont:[UIFont fontWithName:@"Arial" size:16]];
	[imgvBtnSeperate addSubview:m_pLblBadges];
	[m_pLblBadges release];
	
	m_pLblBadgesNumber = [[UILabel alloc] initWithFrame:CGRectMake(nX, nY+15, nW, nH+15)];
	[m_pLblBadgesNumber setBackgroundColor:[UIColor clearColor]];
	[m_pLblBadgesNumber setTextColor:[UIColor blueColor]];
	[m_pLblBadgesNumber setText:@"10"];
	[m_pLblBadgesNumber setFont:[UIFont fontWithName:@"Arial" size:28]];
	[imgvBtnSeperate addSubview:m_pLblBadgesNumber];
	[m_pLblBadgesNumber release];
	
	m_pBtnBadges = [[UIButton alloc] initWithFrame:CGRectMake(nX-8, nY-8, img.size.width, img.size.height)];	
	[m_pBtnBadges setBackgroundColor:[UIColor clearColor]];
	[m_pBtnBadges addTarget:nil action:@selector(onClickBadges) forControlEvents:UIControlEventTouchUpInside];
	[imgvBtnSeperate addSubview:m_pBtnBadges];
	
	////////////////////////
	nX=nX+106;
	m_pLblPosts = [[UILabel alloc] initWithFrame:CGRectMake(nX, nY, nW, nH)];
	[m_pLblPosts setBackgroundColor:[UIColor clearColor]];
	[m_pLblPosts setTextColor:[UIColor blueColor]];
	[m_pLblPosts setText:@"POSTS"];
	[m_pLblPosts setFont:[UIFont fontWithName:@"Arial" size:16]];
	[imgvBtnSeperate addSubview:m_pLblPosts];
	[m_pLblPosts release];
	
	m_pLblPostsNumber = [[UILabel alloc] initWithFrame:CGRectMake(nX, nY+15, nW, nH+15)];
	[m_pLblPostsNumber setBackgroundColor:[UIColor clearColor]];
	[m_pLblPostsNumber setTextColor:[UIColor blueColor]];
	[m_pLblPostsNumber setText:@"10"];
	[m_pLblPostsNumber setFont:[UIFont fontWithName:@"Arial" size:28]];
	[imgvBtnSeperate addSubview:m_pLblPostsNumber];
	[m_pLblPostsNumber release];
	
	
	m_pBtnPosts = [[UIButton alloc] initWithFrame:CGRectMake(nX-8, nY-8, img.size.width, img.size.height)];
	[m_pBtnPosts setBackgroundColor:[UIColor clearColor]];
	[m_pBtnPosts addTarget:nil action:@selector(onClickPost) forControlEvents:UIControlEventTouchUpInside];
	[imgvBtnSeperate addSubview:m_pBtnPosts];
	//table
	int nHRow = 40;
	m_pMainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 235, 320, 4*nHRow) style:UITableViewStylePlain];
	m_pMainTableView.delegate = self;
	m_pMainTableView.dataSource = self;
	m_pMainTableView.rowHeight = nHRow;
	[m_pMainTableView setShowsVerticalScrollIndicator:NO];
	[m_pMainTableView setBackgroundColor:[UIColor clearColor]];
	[self.view addSubview:m_pMainTableView];
	[m_pMainTableView reloadData];
	m_pMainTableView.scrollEnabled = YES;
	
}

-(void)setUserProfile:(NSDictionary *)userProfile{
	m_pUserProfile = [userProfile retain];
	NSString *str = [[NSString alloc] initWithString:[m_pUserProfile objectForKey:kTitle]];
	[m_pLblName setText:str];
	NSString *money = [[NSString alloc] initWithFormat:@"$ %@", [m_pUserProfile objectForKey:kAmount]];
	[m_pLblMoney setText:money];
	
}

-(void)onClickAccount{
	[g_AppDelegate openUpdateView];
}

-(void)onClickBack{
	[g_AppDelegate openFeedView];
}

-(void)onClickFollow{
	isFollow = !isFollow;
	
	if(isFollow)
		[m_pBtnFollow setTitle:@"Following" forState:UIControlStateNormal];
	else
		[m_pBtnFollow setTitle:@"Follow" forState:UIControlStateNormal];
}

-(void)onClickPost{
	[g_AppDelegate openFeedView];
}

-(void)onClickBadges{
	[g_AppDelegate openBadgesView];
}

-(void)onClickHigh{
	[g_AppDelegate openHighView];
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [m_pArrContent count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
	
	UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",IMG_BTN_ARROW]]];
	cell.accessoryView = imageView;
	cell.textLabel.text = [m_pArrContent objectAtIndex:indexPath.row];
	
	//thiet lap gia tri cua cell thong qua m_pUserprofile
	
	return cell;
}



#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	if(indexPath.row == 0){//view perchase
		[g_AppDelegate openFeedView];
	}
	if(indexPath.row == 1){//view  report
		
	}
	if(indexPath.row == 2){//follwers
		[g_AppDelegate openFeaturedMembersWithMembers:m_pArrMembers];
	}
	if(indexPath.row == 3){//following
		[g_AppDelegate openFeaturedMembersWithMembers:m_pArrMembers];
	}
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
